package com.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.UUID;

/**
 * User: shaxj
 * Date: 13-10-28
 * Time: 下午2:21
 */
public class GooFlowServlet extends javax.servlet.http.HttpServlet {

    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String method = request.getParameter("method");
        if (method.equals("save")) {
            this.save(request, response);
        } else if (method.equals("view")) {
            this.view(request, response);
        } else if (method.equals("list")) {
            this.list(request, response);
        }else if (method.equals("delete")) {
            this.delete(request, response);
        }
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/x-text;charset=UTF-8");
        String id = request.getParameter("id");
        String dirPath = this.getDirPath();
        File file = new File(dirPath);
        String[] fileNames = file.list();
        StringBuilder stringBuilder = new StringBuilder("");
        for (String fileName : fileNames) {
            System.out.println("fileName = " + fileName);
            stringBuilder.append(fileName);
            stringBuilder.append(",");
        }

        System.out.println("stringBuilder = " + stringBuilder.length());
        String data = "";
        if (stringBuilder.length() > 0) {
            stringBuilder.setLength(stringBuilder.length() - 1);
        }
        response.getWriter().write(stringBuilder.toString());
        //To change body of created methods use File | Settings | File Templates.
    }

    private void view(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/x-text;charset=UTF-8");
        String id = request.getParameter("id");
        String dirPath = this.getDirPath();
        File file = new File(dirPath, id);
        FileInputStream fileInputStream = new FileInputStream(file);
        DataInputStream dataInputStream = new DataInputStream(fileInputStream);
        String data = dataInputStream.readUTF();
        System.out.println("data = " + data);
        fileInputStream.close();
        dataInputStream.close();
        response.getWriter().write(data);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/x-text;charset=UTF-8");
        String id = request.getParameter("id");
        String dirPath = this.getDirPath();
        File file = new File(dirPath, id);
        if(file.exists()){
            file.delete();
        }
        response.getWriter().write("删除成功！");
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/x-text;charset=UTF-8");
        String data = request.getParameter("data");
        String id = request.getParameter("id");
        String dirPath = this.getDirPath();
        File file = null;
        if (id != null && !id.trim().equals("")) {
            file = new File(dirPath, id);
        } else {
            file = new File(dirPath, UUID.randomUUID().toString() + ".txt");
        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
        dataOutputStream.writeUTF(data);
        fileOutputStream.flush();
        dataOutputStream.flush();
        fileOutputStream.close();
        dataOutputStream.close();
        response.getWriter().write("保存成功");
    }


    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doPost(request, response);
    }

    private String getDirPath() {
        File file = new File(System.getProperty("user.dir") + "\\" + "files");
        if (!file.exists()) {
            file.mkdir();
        }
        return file.getPath();
    }

    public static void main(String[] args) throws IOException, URISyntaxException {
       /* File file = new File("c:\\data.txt");
        DataInputStream dataInputStream = new DataInputStream(new FileInputStream(file));
        String data = dataInputStream.readUTF();
        System.out.println("data = " + data);*/
        System.out.println("System.getProperty(\"user.dir\") = " + System.getProperty("user.dir"));
        File file = new File(System.getProperty("user.dir") + "\\" + "files");
        if (!file.exists()) {
            file.mkdir();
        }
        System.out.println("file.isDirectory() = " + file.isDirectory());
        String[] fileNames = file.list();
        for (String fileName : fileNames) {
            System.out.println("fileName = " + fileName);
        }

        System.out.println("new GooFlowServlet().getDirPath() = " + new GooFlowServlet().getDirPath());
    }
}
